package netty.common.serializer;

import com.zetas.option.push.message.sdk.PushSDKMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.MessageToMessageEncoder;

import java.net.InetSocketAddress;
import java.util.List;

public class UdpEncoder extends MessageToMessageEncoder<PushSDKMessage.Message4Backend> {

	private final String host = System.getProperty("serverHost", "127.0.0.1");
	private final InetSocketAddress address = new InetSocketAddress(host, 7775);

	@Override
	protected void encode(ChannelHandlerContext ctx, PushSDKMessage.Message4Backend msg, List<Object> out) throws Exception {
		DatagramPacket datagramPacket = new DatagramPacket(
				ctx.alloc().buffer().writeBytes(msg.toByteArray()),
				address);

		out.add(datagramPacket);
	}
}
